package com.ctgu.springshiro.dao;

import com.ctgu.springshiro.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface UserDao {

    @Select("select * from user where id = #{userId}")
    @Results(id = "userMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "age", column = "age"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email"),
            @Result(property = "roles",
                    column = "id",
                    many = @Many(select = "com.ctgu.springshiro.dao.RoleDao.getRolesByUserId",
                            fetchType = FetchType.LAZY))
    })
    User findById(Integer userId);

    @Select("select * from user where username = #{username}")
    @ResultMap(value = "userMap")
    User findByName(String username);

    @Select("select * from user")
    @ResultMap(value = "userMap")
    List<User> getAllUser();

    @Update("update user set username=#{username} where id=#{id}")
    void updateUser(User user);

    @Delete("delete from user where id =#{id}")
    void deleteUser(User user);
}

